package com.nny.alarm.edit;


import java.text.DateFormatSymbols;
import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.nny.alarm.R;
import com.uxwine.alarm.TimeCycle2;
import com.uxwine.cmm.UxAdapter;
import com.uxwine.widget.ActionDialog;

public class CycleWeekDialog extends ActionDialog implements 
		View.OnClickListener, OnItemClickListener
{

	CycleEditor mCycleEditor;
	AlarmEditor mEditor;
	ListView mList;
	MAdapter mAdapter;
	long mlFlagRecord;
	
	public CycleWeekDialog(AlarmEditor editor, CycleEditor cycleEditor)
	{
		super(editor.mCtx, R.style.NomalDialog);
		mEditor = editor;
		mCycleEditor = cycleEditor;
	}			
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dlg_alarm_cycle_week);
		setCanceledOnTouchOutside(true);
		
		final Context context = getContext();
		findViewById(R.id.alarm_cycle_ok).setOnClickListener(this);
		findViewById(R.id.alarm_cycle_cancel).setOnClickListener(this);
		
		mList = (ListView)findViewById(R.id.alarm_cycle_week_list);
		mAdapter = new MAdapter(mEditor.mAlarm.cycle);
		mList.setAdapter(mAdapter);
		mList.setOnItemClickListener(this);
	}	
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.alarm_cycle_ok)
		{
			if(mEditor != null)
			{
				mEditor.mAlarm.cycle.from(mAdapter.mCycle);
				mCycleEditor.mlFlagWeek = mAdapter.mCycle.getFlags();
			}
			exit(ACTION_OK);
		}
		else if(id == R.id.alarm_cycle_cancel)
		{
			exit(ACTION_CANCEL);
		}
	}
	
	private class MAdapter extends UxAdapter<String>
	{
		DateFormatSymbols mDateFmt;
		LayoutInflater mInflater;
		TimeCycle2 mCycle;
		
		public MAdapter(TimeCycle2 tc)
		{
			mDateFmt = new DateFormatSymbols();
			mData = new ArrayList<String>();
			final String[] weeks = mDateFmt.getWeekdays();
			mCycle = new TimeCycle2(tc.getCycle(), mCycleEditor.mlFlagWeek);
			for(int i = 0; i < 7; i++)
			{
				mData.add(weeks[i+1]);
			}
			mInflater = LayoutInflater.from(getContext());
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			String name = (String)getItem(position);
			convertView = mInflater.inflate(R.layout.listitem_alarm_cycle_week, null);
			TextView tv = (TextView)convertView.findViewById(R.id.alarm_cycle_tv);
			CheckBox cb = (CheckBox)convertView.findViewById(R.id.alarm_cycle_cb);
			tv.setText(name);
			cb.setChecked(mCycle.isWeekSet(position));
			convertView.setTag(mCycle);
			return convertView;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,	long id)
	{
		TimeCycle2 c = (TimeCycle2)view.getTag();
		c.setWeekDay(position, !c.isWeekSet(position));
		mAdapter.notifyDataSetChanged();
	}
}
